﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using log4net;
using NHibernate.Criterion;
using BidFunLib.Entity;


namespace BidFunLib.DAO
{
    class DayDAO
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(DayDAO));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        public Day getDayByLabel(String label)
        {
            Day day = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<Day> days = (IList<Day>)session.CreateQuery("from Day d where d.Day= ?")
                .SetString(0, label)
                .List<Day>();

                if (days != null && days.Count > 0)
                {
                    day = days.First();
                }
                transaction.Commit();
                session.Close();
                return day;
            }
        }


        public Day getDayByEnum(BidFunLib.Services.DayManager.DAYS enumDay)
        {
            Day day = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<Day> days = (IList<Day>)session.CreateCriteria(typeof(Day)).Add(Expression.Like("DayLabel", enumDay.ToString())).List<Day>();
                if (days != null && days.Count > 0)
                {
                    day = days.First();
                }
                transaction.Commit();
                session.Close();
                return day;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Day getDayByID(Int32 id)
        {
            Day day = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<Day> days = (IList<Day>)session.CreateQuery("from Day d where d.IdDay= ?")
                .SetInt32(0, id)
                .List<Day>();

                if (days != null && days.Count > 0)
                {
                    day = days.First();
                }
                transaction.Commit();
                session.Close();
            }
            return day;
        }

        public void saveDay(Day day)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                log.Debug("Saving new Days " + day.DayLabel);
                Int32 newID = (Int32)session.Save(day);
                log.Debug("new Day " + day.DayLabel+ " saved !");
                transaction.Commit();
                session.Close();
            }
        }

    }
}
